package com.example.statistics.ui.yw

import android.os.Bundle
import com.example.statistics.R
import com.example.statistics.bean.yw.ProductTypeBean
import com.example.statistics.common.BaseLoadingListActivity
import com.example.statistics.ex.httpGet
import com.example.statistics.ex.onActionLongClicked
import com.example.statistics.net.UrlConfig
import com.example.statistics.utils.Constants
import com.example.statistics.utils.activityFinish
import com.example.statistics.utils.activityForResult
import com.jhj.slimadapter.SlimAdapter
import com.jhj.slimadapter.holder.ViewInjector

class ProductTypeListActivity : BaseLoadingListActivity<ProductTypeBean>() {

    var action = ""

    override val title: String
        get() = "成品种类"

    override val url: String
        get() = UrlConfig.YW_PRODUCT_TYPE_LIST

    override val itemLayoutRes: Int
        get() = R.layout.list_item_common_display


    override fun itemViewConvert(adapter: SlimAdapter, injector: ViewInjector, bean: ProductTypeBean, position: Int) {
        injector
            .text(R.id.tv_title, bean.productBrand + bean.productName)
            .text(R.id.tv_subtitle, "${bean.inventoryNumber}件 | ${bean.inventoryWeight}KG")
            .clicked {
                if (action == Constants.SELECTOR) {
                    activityFinish(Constants.DATA to bean)
                }
            }
            .onActionLongClicked(
                "销售" to {
                    activityForResult<ProductSalesActivity>(Constants.DATA to bean) {
                        refresh()
                    }
                },
                "删除" to {
                    httpGet<Any>(UrlConfig.YW_PRODUCT_TYPE_DELETE, config = {
                        isShowDialog = true
                        listParams = listOf("id" to bean.ID)
                    }) {
                        refresh()
                    }
                }
            )
    }

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        action = intent.getStringExtra(Constants.ACTION).orEmpty()
        topBarRightImage {
            activityForResult<ProductTypeAddActivity>() {
                refresh()
            }
        }
    }


}